﻿using DataBaseSync.Pages;
using DataBaseSync.Vendor.DataService;
using DataBaseSync.Vendor.Log;
using Newtonsoft.Json.Linq;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;


namespace DataBaseSync
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private List<Page> Pages=new List<Page>();
        public Logger _logger = new Logger();

        /// <summary>
        /// 当前展示的子页面
        /// </summary>
        public Page prop_page { get; set; }
        /// <summary>
        /// 当前按钮高亮
        /// </summary>
        public Button prev_clickedbtn;

        public event PropertyChangedEventHandler? PropertyChanged;

        public MainWindow()
        {
            this.DataContext = this;
            InitializeComponent();
            Pages.Add(new PageTask(_logger));
            Pages.Add(new PageSetting());

            _frame.NavigationUIVisibility = System.Windows.Navigation.NavigationUIVisibility.Hidden;
            UpdateFrameLive(Pages[0]);
            prev_clickedbtn = btntask;
            prev_clickedbtn.IsEnabled = false;

             
        }
        public void UpdateFrameLive(Page obj)
        {
            prop_page = obj;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("prop_page"));
        }

        private void btnclose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void Border_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if(e.LeftButton==MouseButtonState.Pressed)
            {
                DragMove();
            }
        }

        private void btnmin_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }

        private void btnmax_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState ^ WindowState.Maximized;
        }

        private void btnopt_click(object sender, RoutedEventArgs e)
        {
            ((Button)sender).IsEnabled = false;

            prev_clickedbtn.IsEnabled = true;
            prev_clickedbtn = (Button)sender;
            UpdateFrameLive(Pages[int.Parse(((Button)sender).Tag.ToString())]);
        }

       
        private void UpdateDBStatus(int idx,int status)
        {
            
            this.Dispatcher.Invoke(new Action( () =>
            {

                if (idx == 0 && status==0)
                    mysqlconnstate.Fill = (Brushes.Green);
                if (idx == 1 && status == 0)
                    oracleconnstate.Fill = (Brushes.Green);

            }));
            
        }


        private void Main_Loaded(object sender, RoutedEventArgs e)
        {
            this.Dispatcher.Invoke(() =>
            {
                Task.Run(() =>
                {
                    UpdateDBStatus(0, MysqlService.Connect());
                    UpdateDBStatus(1, OracleService.Connect());
                });
            });
        }
    }
}
